#include "stdafx.h"
#include "Pet.h"
/*Pet::Pet()
{
	m_nPetId = 0;
	m_pPetCSVInfo = NULL;
	LOGGER_INFO("Pet:%p,m_nPetId:%d",this,m_nPetId);
}*/

Pet::Pet(PetId nPetId)
{
	m_nPetId = nPetId;
	m_pPetCSVInfo = NULL;
	m_bWatch = false;
	m_pOwnerPlayer = NULL;

	m_nNextPointHP = 0;
	m_nNextPointMoneyHP = 0;
	m_nNextPointAP = 0;
	m_nNextPointMoneyAP = 0;
	m_nNextPointDP = 0;
	m_nNextPointMoneyDP = 0;
	m_nNextPointSP = 0;
	m_nNextPointMoneySP = 0;

	LOGGER_INFO("Pet(PetId nPetId):%p,m_nPetId:%d",this,m_nPetId);
}
Pet::~Pet()
{
	LOGGER_INFO("~Pet:%p,m_nPetId:%d",this,m_nPetId);
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		singleton<SkillManager>::instance().Remove(m_arSkills[i]);
		LOGGER_INFO("~Pet:%p,m_arSkills:%d",this,m_arSkills[i]);
	}
}
void Pet::SetOwnerPlayer(Player* pPlayer)
{
	m_pOwnerPlayer = (void*)pPlayer;
}
Player* Pet::GetOwnerPlayer()
{ 
	return (Player*)m_pOwnerPlayer;
};
void Pet::addSkill(Int32 csv_id)
{
	if (m_arSkills.size() >= PetInfo::MAX_PET_SKILLS_SIZE)
	{
		LOGGER_ERROR("addSkill,m_arSkills.size() >= PetInfo::MAX_PET_SKILLS_SIZE");
		return;
	}

	Skill *pSkill = singleton<SkillManager>::instance().CreateFromCSVId(csv_id);
	if (pSkill == NULL)
	{
		LOGGER_ERROR("addSkill,pSkill == NULL,csv_id:%d",csv_id);
		return;
	}
	m_arSkills.push_back(pSkill->GetSkillId());
	LOGGER_INFO("addSkill,pSkill->GetSkillId():%d",pSkill->GetSkillId());
}
void Pet::removeSkill(Int32 csv_id)
{
	std::vector<SkillId>::iterator iter = m_arSkills.begin();
	std::vector<SkillId>::iterator iterend = m_arSkills.end();
	for (; iter != iterend; iter++)
	{
		Skill *pSkill = singleton<SkillManager>::instance().Get(*iter);
		if (pSkill != NULL)
		{
			if(pSkill->GetSkillInfo().csv_id == csv_id)
			{
				singleton<SkillManager>::instance().Remove(*iter);
				m_arSkills.erase(iter);
				return;
			}
		}
	}
}
bool Pet::hasSkill(Int32 csv_id)
{
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		Skill *pSkill = singleton<SkillManager>::instance().Get(m_arSkills[i]);
		if (pSkill != NULL)
		{
			if(pSkill->GetSkillInfo().csv_id == csv_id)
				return true;
		}
	}
	return false;
}
Skill* Pet::getSkill(Int32 csv_id)
{
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		Skill *pSkill = singleton<SkillManager>::instance().Get(m_arSkills[i]);
		if (pSkill != NULL)
		{
			if(pSkill->GetSkillInfo().csv_id == csv_id)
				return pSkill;
		}
	}
	return NULL;
}
void Pet::decodeSkills()
{
	if (m_stPetInfo.m_nSkillsSize > PetInfo::MAX_PET_SKILLS_SIZE)
	{
		LOGGER_ERROR("decodeSkills,m_stPetInfo.m_nSkillsSize > PetInfo::MAX_PET_SKILLS_SIZE");
		return;
	}
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		singleton<SkillManager>::instance().Remove(m_arSkills[i]);
	}
	m_arSkills.clear();
	Skill *pSkill = NULL;
	for (UInt32 i = 0; i < m_stPetInfo.m_nSkillsSize; i++)
	{
		if (m_stPetInfo.m_arSkills[i].csv_id != 0 )
		{
			pSkill = singleton<SkillManager>::instance().CreateFromSkillInfo(m_stPetInfo.m_arSkills[i]);
			if (pSkill == NULL)
			{
				LOGGER_ERROR("decodeSkills,m_stPetInfo.m_arSkills[i].csv_id != 0,but pSkill == NULL,csv_id:%d",m_stPetInfo.m_arSkills[i].csv_id);
			}
			else
			{
				m_arSkills.push_back(pSkill->GetSkillId());
				LOGGER_INFO("decodeSkills,pSkill->GetSkillId():%d",pSkill->GetSkillId());
			}
		}
	}
	if (hasSkill(SKILL_ID_TP)) removeSkill(SKILL_ID_TP);
}
void Pet::encodeSkills()
{
	if (m_arSkills.size() > PetInfo::MAX_PET_SKILLS_SIZE)
	{
		LOGGER_ERROR("encodeSkills,m_arSkills.size() >= PetInfo::MAX_PET_SKILLS_SIZE");
		return;
	}

	m_stPetInfo.m_nSkillsSize = m_arSkills.size();
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		Skill *pSkill = singleton<SkillManager>::instance().Get(m_arSkills[i]);
		if (pSkill != NULL)
		{
			m_stPetInfo.m_arSkills[i] = pSkill->GetSkillInfo();
		}
	}
}
void Pet::sendSkills(PacketEncoder &encoder)
{
	//if (!hasSkill(SKILL_ID_JD))addSkill(SKILL_ID_JD);
	//if (!hasSkill(SKILL_ID_FY))addSkill(SKILL_ID_FY);
	encoder.AddItem(Int32(m_arSkills.size()));
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		Skill *pSkill = singleton<SkillManager>::instance().Get(m_arSkills[i]);
		if (pSkill == NULL) continue;

		encoder.AddItem(pSkill->GetSkillInfo().csv_id);
		encoder.AddItem(pSkill->GetSkillInfo().level);
	}
}

void Pet::addEXP(Int32 nValue)
{
	m_stPetInfo.m_nEXP += nValue;
	if (m_stPetInfo.m_nEXP < 0) m_stPetInfo.m_nEXP = 0;
	while (m_stPetInfo.m_nEXP >= m_stPetInfo.m_nEXPMax) 
	{
		if (m_stPetInfo.m_nEXPMax <= 0)
		{
			break;
		}
		m_stPetInfo.m_nLevel++;
		m_stPetInfo.m_nEXP = m_stPetInfo.m_nEXP - m_stPetInfo.m_nEXPMax;
		updateMaxValue();
	}
	Player* pPlayer = GetOwnerPlayer();
	if (pPlayer != NULL)
	{
		pPlayer->PrepareSendPets();
	}
}
void Pet::updateMaxValue()
{
	std::string path = singleton<GameServer>::instance().GetServerPath();
	std::string filename = path + "/lua/pet.lua";
	Player* pPlayer = GetOwnerPlayer();
	if (pPlayer == NULL)
		return;
	Script* pScript = singleton<ScriptManager>::instance().GetPlayerScript(filename);
	if(pScript)
	{

		Int32 nRet = pPlayer->luaCallFunction1(pScript,"getEXPMaxByLevel",m_stPetInfo.m_nLevel);
		if(nRet != -1)m_stPetInfo.m_nEXPMax = nRet;

		nRet = pPlayer->luaCallFunction3(pScript,"getHPMaxByLevel",m_stPetInfo.m_nLevel,m_stPetInfo.m_nPointHP,m_stPetInfo.m_nGrowth);
		if(nRet != -1)m_stPetInfo.m_nHP = nRet;
		nRet = pPlayer->luaCallFunction3(pScript,"getHPMaxByLevel",m_stPetInfo.m_nLevel,m_stPetInfo.m_nPointHP+1,m_stPetInfo.m_nGrowth);
		if(nRet != -1)m_nNextPointHP = nRet;
		nRet = pPlayer->luaCallFunction1(pScript,"getStrengthenMoney",m_stPetInfo.m_nPointHP);
		if(nRet != -1)m_nNextPointMoneyHP = nRet;

		nRet = pPlayer->luaCallFunction3(pScript,"getAPByLevel",m_stPetInfo.m_nLevel,m_stPetInfo.m_nPointAP,m_stPetInfo.m_nGrowth);
		if(nRet != -1)m_stPetInfo.m_nAP = nRet;
		nRet = pPlayer->luaCallFunction3(pScript,"getAPByLevel",m_stPetInfo.m_nLevel,m_stPetInfo.m_nPointAP+1,m_stPetInfo.m_nGrowth);
		if(nRet != -1)m_nNextPointAP = nRet;
		nRet = pPlayer->luaCallFunction1(pScript,"getStrengthenMoney",m_stPetInfo.m_nPointAP);
		if(nRet != -1)m_nNextPointMoneyAP = nRet;

		nRet = pPlayer->luaCallFunction3(pScript,"getDPByLevel",m_stPetInfo.m_nLevel,m_stPetInfo.m_nPointAP,m_stPetInfo.m_nGrowth);
		if(nRet != -1)m_stPetInfo.m_nDP = nRet;
		nRet = pPlayer->luaCallFunction3(pScript,"getDPByLevel",m_stPetInfo.m_nLevel,m_stPetInfo.m_nPointAP+1,m_stPetInfo.m_nGrowth);
		if(nRet != -1)m_nNextPointDP = nRet;
		nRet = pPlayer->luaCallFunction1(pScript,"getStrengthenMoney",m_stPetInfo.m_nPointAP);
		if(nRet != -1)m_nNextPointMoneyDP = nRet;

		nRet = pPlayer->luaCallFunction3(pScript,"getSPByLevel",m_stPetInfo.m_nLevel,m_stPetInfo.m_nPointSP,m_stPetInfo.m_nGrowth);
		if(nRet != -1)m_stPetInfo.m_nSP = nRet;
		nRet = pPlayer->luaCallFunction3(pScript,"getSPByLevel",m_stPetInfo.m_nLevel,m_stPetInfo.m_nPointSP+1,m_stPetInfo.m_nGrowth);
		if(nRet != -1)m_nNextPointSP = nRet;
		nRet = pPlayer->luaCallFunction1(pScript,"getStrengthenMoney",m_stPetInfo.m_nPointSP);
		if(nRet != -1)m_nNextPointMoneySP = nRet;
	}
}